<template>
	<!-- 活动中转页 -->
	<div class="con">
		<van-loading color="#f70114" vertical size="36px" v-if="loading">加载中~</van-loading>
		<i class="icon iconfont icon-shuaxin" @click="login()" v-else><span>点击刷新</span></i>
	</div>
</template>

<script>
import store from 'store';
import { Toast } from 'vant';
import moment from 'moment';
export default {
	components: {},
	data() {
		return {
			loading: false
		};
	},
	methods: {
		// 翼支付授权登录
		login() {
			this.loading = true;
			// 获取翼支付授权
			let params = {
				scope: 'user_info,mobile', // 需要授权获取的内容
				clientId: '86000000162', // 第三方应用编号
				redirectUrl: 'http://zfx.xmonecode.com', // 回调地址,必须和申请应用是填写对一致
				merchantCode: '3178034478305436', // 商户号
				clientName: '一码当先', // 商户名称
				state: 'bestpay' // 第三方平台自定义参数，用户授权后会原样回传给第三方平台
			};
			console.log(moment().format('hh:mm:ss'));
			window.BestpayHtml5.User.userAuth(
				params,
				res => {
					this.$baiduPush('授权登录事件', 'tap', '授权成功');
					console.log(moment().format('hh:mm:ss'));
					this.mallLogin({
						code: res.code,
						grantType: res.grantType,
						refreshToken: res.refreshToken || ''
					});
				},
				err => {
					// 错误处理
					this.loading = false;
					this.$baiduPush('授权登录事件', 'tap', '授权错误');
				},
				err => {
					// 退出子应用
					this.$baiduPush('授权登录事件', 'tap', '授权取消');
					window.BestpayHtml5.APP.exitSubApp();
				}
			);
		},
		// 登录自营
		mallLogin(params) {
			this.loading = true;
			this.$service.common.common
				.selfmallLogin2(params)
				.then(res => {
					console.log(moment().format('hh:mm:ss'));
					return;
					const id =
						decodeURIComponent(
							(new RegExp('[?|&]' + 'id' + '=' + '([^&;]+?)(&|#|;|$)').exec(window.location.href) || [, ''])[1].replace(/\+/g, '%20')
						) || null;
					store.set('selfInfo', res);
					window.location.href = `https://zfx-dev.xmonecode.com/和尚/index.html?id=${id}&token=${store.get('token')}#/squeeze/ticket`;
					// window.location.href = `https://zfx.xmonecode.com?id=${id}&token=${getStore('token')}#/squeeze/ticket`;
				})
				.catch(err => {
					Toast('自营登录失败!!!');
					this.loading = false;
				});
		}
	},
	created() {
		console.log('进页面', moment().format('hh:mm:ss'));

		this.login();
	}
};
</script>

<style lang="scss" scoped>
.con {
	height: 100vh;
	display: flex;
	justify-content: center;
	align-items: center;
	.icon-shuaxin {
		font-size: 56px;
		text-align: center;
	}
	span {
		font-size: 24px;
		display: block;
	}
}
</style>
